package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

import lib.Ensemble;
import vue.composants.*;

public class FenetrePrincipale extends JFrame implements ActionListener{

	private static final long serialVersionUID = 1L;
	private ArrayList<Ensemble> mesEnsembles;
	private Boutton bouttonAjoutEnsemble;
	private Boutton bouttonIntersection;
	private Boutton bouttonComplementaire;
	private Boutton bouttonRemplissageAleatoire;
	private Boutton bouttonAfficheEnsemble;
	private Boutton bouttonAjouteElement;
	private JPanel panOption;
	private JPanel panEnsemble;
	public JPanel getPanEnsemble() {
		return panEnsemble;
	}

	public void setPanEnsemble(JPanel panEnsemble) {
		this.getContentPane().remove(this.panEnsemble);
		this.panEnsemble = panEnsemble;
		this.panEnsemble.setBorder(new LineBorder(new Color(50,100,150),1,true));
		panEnsemble.setFocusable(false);
		this.getContentPane().add(this.panEnsemble,BorderLayout.CENTER);
		this.setVisible(false);
		this.repaint();
		
		this.setVisible(true);
	}

	private JTextArea console;
	
	public FenetrePrincipale()
	{
		super("Ensemble");
		this.initialiseFenetre();
	}
	
	private void initialiseFenetre()
	{
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(1000, 1000);
		
		this.setConsoleToTextArea();
		
		this.panOption = new JPanel();
		this.panOption.setBorder(new LineBorder(new Color(50,100,150),1,true));
		
		this.panEnsemble = new JPanel();
		this.panEnsemble.setBorder(new LineBorder(new Color(50,100,150),1,true));
		
		this.panEnsemble.add(this.console);
		
		this.bouttonIntersection = new Boutton("Intersection");
		this.bouttonIntersection.addActionListener(this);
		
		this.bouttonComplementaire = new Boutton("Complementaire");
		this.bouttonComplementaire.addActionListener(this);
		
		this.bouttonRemplissageAleatoire = new Boutton("Remplissage Aleatoire");
		this.bouttonRemplissageAleatoire.addActionListener(this);
		
		this.bouttonAjoutEnsemble = new Boutton("Nouvel ensemble");
		this.bouttonAjoutEnsemble.addActionListener(this);
		
		this.bouttonAfficheEnsemble = new Boutton("Affiche ensemble");
		this.bouttonAfficheEnsemble.addActionListener(this);
		
		this.bouttonAjouteElement = new Boutton("Ajouter element");
		this.bouttonAjouteElement.addActionListener(this);
		
		this.panOption.setLayout(new FlowLayout());
		this.panOption.add(this.bouttonIntersection);
		this.panOption.add(this.bouttonComplementaire);
		this.panOption.add(this.bouttonRemplissageAleatoire);
		this.panOption.add(this.bouttonAjoutEnsemble);
		this.panOption.add(this.bouttonAfficheEnsemble);
		this.panOption.add(this.bouttonAjouteElement);
		
		this.getContentPane().setLayout(new GridLayout());
		this.getContentPane().add(this.panOption);
		this.getContentPane().add(this.panEnsemble);
		
		this.mesEnsembles = new ArrayList<Ensemble>();
		
		this.setVisible(true);
	}
	
	private void setConsoleToTextArea()
	{
		this.console = new JTextArea();
		PrintStream out = new PrintStream( new PanelConsole( this.console ) );
		System.setOut( out );
	}
	
	private void nouvelEnsemble()
	{
		new NouvelEnsembleDialog(this);
	}
	
	private void Complementaire()
	{
		new ComplementaireDialog(this);
	}
	
	private void Intersection()
	{
		new IntersectionDialog(this);
	}
	
	private void RemplissageAleatoire()
	{
		new RemplissageAleatoireDialog(this);
	}

	private void AfficheEnsemble()
	{
		new AfficheEnsembleDialog(this);
	}
	
	private void AjouteElement()
	{
		new AjouteElementDialog(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==this.bouttonAjoutEnsemble)
		{
			this.nouvelEnsemble();
		}
		else if(e.getSource()==this.bouttonComplementaire)
		{
			this.Complementaire();
		}
		else if(e.getSource()==this.bouttonIntersection)
		{
			this.Intersection();
		}
		else if(e.getSource()==this.bouttonRemplissageAleatoire)
		{
			this.RemplissageAleatoire();
		}
		else if(e.getSource()==this.bouttonAfficheEnsemble)
		{
			this.AfficheEnsemble();
		}
		else if(e.getSource()==this.bouttonAjouteElement)
		{
			this.AjouteElement();
		}
	}

	/**
	 * @return the mesEnsemble
	 */
	public ArrayList<Ensemble> getMesEnsemble() {
		return mesEnsembles;
	}

	/**
	 * @param mesEnsemble the mesEnsemble to set
	 */
	public void setMesEnsemble(ArrayList<Ensemble> mesEnsemble) {
		this.mesEnsembles = mesEnsemble;
	}
	
}
